#!/bin/sh
#
# This is part of OpenIPC.org project
#

# SoC detect
mem_start=0x80000000
mem_total=$(fw_printenv -n totalmem | tr -d 'M')
mem_total=${mem_total:=64}

os_mem_size=$(fw_printenv -n osmem | tr -d 'M')
os_mem_size=${os_mem_size:=32}

mmz_start=0
mmz_size=0

MEM_ALL=256
MEM_START=0x800
MEM_SAMALL_LIN=0x1
MEM_MODE=0
b_arg_demb=1
VOU_LEN=22

report_error() {
  echo "******* Error: There's something wrong, please check! *****"
  exit 1
}

insert_wdt() {
  insmod wdt.ko
}

remove_wdt() {
  rmmod wdt.ko
}

insert_audio() {
  insmod hi3536dv100_aio.ko
  insmod hi3536dv100_ai.ko
  insmod hi3536dv100_ao.ko
  insmod hi3536dv100_aenc.ko
  insmod hi3536dv100_adec.ko
  insmod hi_acodec.ko
}

remove_audio() {
  rmmod hi_acodec.ko
  rmmod hi3536dv100_adec
  rmmod hi3536dv100_aenc
  rmmod hi3536dv100_ao
  rmmod hi3536dv100_ai
  rmmod hi3536dv100_aio
}

insert_ko() {
  HVRMEM=`cat /proc/cmdline  | awk '{print $1}' | sed s/mem=//`
  HVRMEM_OS=`echo $HVRMEM | sed s/M//`

  MEM_LEN=`echo "$MEM_ALL $HVRMEM_OS $VOU_LEN"|awk '{printf("%d",$1-$2-$3)}'`M
  VOU_START=`echo "$MEM_START $HVRMEM_OS $MEM_LEN "|awk '{printf("0x%03x00000",$1+$2+$3)}'`
  MEM_START=`echo "$MEM_START $MEM_SAMALL_LIN $HVRMEM_OS"|awk '{printf("0x%03x00000",$1+$2*$3)}'`
  VOU_LEN=`echo $VOU_LEN`M

  echo MEM_START=$MEM_START HVRMEM=$HVRMEM MEM_LEN=$MEM_LEN VOU_START=$VOU_START VOU_LEN=$VOU_LEN

  insmod hi_osal.ko mmz=anonymous,0,$mmz_start,$mmz_size:vou,0,$VOU_START,$VOU_LEN anony=1 || report_error
  insmod hi3536dv100_base.ko
  insmod hi3536dv100_sys.ko mem_total=$mem_total
  if [[ $? -ne 0 ]]; then
    exit;
  fi

  insmod hi3536dv100_vdec.ko VBSource=0 u32ProtocolSwitch=0 StreamCompaMode=1 MiniBufMode=0
  insmod hi3536dv100_vfmw.ko 
  insmod hi3536dv100_jpegd.ko
  insmod hi3536dv100_tde.ko
  insmod hi3536dv100_region.ko
  insmod hi3536dv100_vgs.ko

  insmod hi3536dv100_vpss.ko vpss_vb_source=0 vpss_en_ratio=0
  insmod hi3536dv100_vou.ko bSaveBufMode=1
  insmod hifb.ko video="hifb:vram0_size:8100,vram1_size:128" softcursor="off"
  insmod hi3536dv100_hdmi.ko
  insmod hi3536dv100_venc.ko
  insmod hi3536dv100_chnl.ko
  insmod hi3536dv100_jpege.ko

  insert_audio
  insert_wdt
  remove_wdt
}

remove_ko() {
  remove_audio
  remove_wdt

  rmmod hi3536dv100_jpege
  rmmod hi3536dv100_chnl
  rmmod hi3536dv100_venc
  rmmod hi3536dv100_hdmi
  rmmod hifb
  rmmod hi3536dv100_vou
  rmmod hi3536dv100_vpss
  rmmod hi3536dv100_vgs

  rmmod hi3536dv100_region
  rmmod hi3536dv100_tde
  rmmod hi3536dv100_jpegd
  rmmod hi3536dv100_vfmw
  rmmod hi3536dv100_vdec
  rmmod hi3536dv100_sys
  rmmod hi3536dv100_base
  rmmod hi_osal
}

load_usage() {
  echo "Usage:  ./load_hisilicon [-option]"
  echo "options:"
  echo "  -i  - insert modules"
  echo "  -r  - remove modules"
  echo "  -a  - remove first and then insert modules"
}

calc_mmz_info() {
  mmz_start=`echo "$mem_start $os_mem_size" | 
  awk 'BEGIN { temp = 0; }
  {
    temp = $1/1024/1024 + $2;
  }
  END { printf("0x%x00000\n", temp); }'`

  mmz_size=`echo "$mem_total $os_mem_size" | 
  awk 'BEGIN { temp = 0; }
  {
    temp = $1 - $2;
  } 
  END { printf("%dM\n", temp); }'`
  echo "mmz_start: $mmz_start, mmz_size: $mmz_size"
}


######################parse arg###################################
b_arg_insmod=0
b_arg_remove=0

for arg in $@; do
  case $arg in
  "-i")
    b_arg_insmod=1
  ;;
  "-r")
    b_arg_remove=1
  ;;
  "-a")
    b_arg_insmod=1
    b_arg_remove=1
  ;;
  "-h")
    load_usage
  ;;
  esac
done
#######################parse arg end########################

if [ $os_mem_size -ge $mem_total ]; then
  echo "[err] os_mem[$os_mem_size], over total_mem[$mem_total]"
  exit
fi

calc_mmz_info

#######################Action###############################

if [ $# -lt 1 ]; then
  load_usage
  exit 0
fi

# - Remove modules
if [ $b_arg_remove -eq 1 ]; then
  remove_ko
fi

# - Insert modules
if [ $b_arg_insmod -eq 1 ]; then
  cd /lib/modules/4.9.37/hisilicon
  insert_ko
fi
